package com.itheima.reggie.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLNonTransientException;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    //一个方法处理一类异常
    @ExceptionHandler(SQLNonTransientException.class)
    public R<String> handler1(SQLNonTransientException ex) {
        //将错误信息打印到控制台
        ex.printStackTrace();
        return R.error(ex.getMessage().split(" ")[2] + "已存在");
    }

    @ExceptionHandler(CustomerException.class)
    public R<String> handler2(CustomerException ex) {
        return R.error(ex.getMessage());
    }

}
